package com.upu173.music.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.andbase.library.app.base.AbBaseFragment;
import com.andbase.library.utils.AbStrUtil;
import com.andbase.library.view.recycler.AbFooterView;
import com.andbase.library.view.recycler.AbRecyclerView;
import com.andbase.library.view.recycler.AbSpaceItemVerticalDecoration;
import com.upu173.music.R;
import com.upu173.music.activity.MusicBaseActivity;
import com.upu173.music.activity.MusicMainActivity;
import com.upu173.music.adapter.PlayListRecyclerViewAdapter;
import com.upu173.music.adapter.SongRecyclerViewAdapter;
import com.upu173.music.global.PlayerData;
import com.upu173.music.model.PlayList;
import com.upu173.music.model.Song;
import com.upu173.music.utils.MusicUtil;

import java.util.ArrayList;
import java.util.List;


public class PlayListFragment extends AbBaseFragment {

    private MusicMainActivity activity;
    public List<PlayList> items = null;
    public PlayListRecyclerViewAdapter recyclerViewAdapter = null;
    private AbRecyclerView recyclerView = null;
    private FrameLayout rootView = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = (FrameLayout)inflater.inflate(R.layout.music_fragment_song_list, container, false);

        activity = (MusicMainActivity)this.getActivity();

        items  = new ArrayList<>();

        recyclerViewAdapter = new PlayListRecyclerViewAdapter(activity,items);

        recyclerView = (AbRecyclerView)rootView.findViewById(R.id.recycler_list);

        recyclerView.addItemDecoration(new AbSpaceItemVerticalDecoration(activity,1));

        recyclerView.setRecyclerViewAdapter(recyclerViewAdapter);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    public void loadData(){
        items.clear();
        List<PlayList> playLists = MusicUtil.getPlayList(activity);
        if(playLists!=null && playLists.size() > 0){
            items.addAll(playLists);
            hideEmptyView(rootView);
        }else{
            showEmptyView(rootView,R.layout.music_view_no_play_list);

            Button addButton = (Button)rootView.findViewById(R.id.add_button);

            addButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.showFragment(1);
                }
            });

        }
        recyclerViewAdapter.notifyDataSetChanged();
        activity.initPlayList(playLists);

    }

    public void removeItem(int position){
        items.remove(position);
        PlayerData.playList.remove(position);
        if(items!=null && items.size() > 0){
            hideEmptyView(rootView);
        }else{
            showEmptyView(rootView,R.layout.music_view_no_play_list);
        }
        recyclerViewAdapter.notifyItemRemoved(position);
        recyclerViewAdapter.notifyItemRangeChanged(position,items.size());
    }
}
